﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class UpgradesPanel : MonoBehaviour {

	public float refuelCost;
	public float repairCost;
	
	public float refuelAmount;
	public float repairAmount;
	
	public Slider fuelSlider;
	public Slider hullSlider;
	
	
	public Slider carrySlider;
	public Slider fuelUpgradeSlider;
	public Slider hullUpgradeSlider;
	
	public Text fuelCost;
	public Text hullCost;
	
	
	public float fuelUpgradeCost;
	public float hullUpgradeCost;
	public float carryUpgradeCost;
	
	public float hullUpgrade;
	public float fuelUpgrade;
	public float carryUpgrade;
	

	public Text fuelUpgradeCostText;
	public Text hullUpgradeCostText;
	public Text carryUpgradeCostText;
	
	
	public PlayerStorage Main;
	

	void Start()
	{
		hullSlider.maxValue = Main.maxhull;
		fuelSlider.maxValue = Main.maxfuel;
		
		carrySlider.maxValue = Main.carryUpgradesMax;
		fuelUpgradeSlider.maxValue = Main.fuelUpgradesMax;
		hullUpgradeSlider.maxValue = Main.hullUpgradesMax;
		
		Main.fuel = PlayerPrefs.GetFloat("Fuel");
		Main.hull = PlayerPrefs.GetFloat("Hull");
		if (Main.hull < 0)
		{
			Main.hull = 50;
		}
		if (Main.fuel < 0)
		{
			Main.fuel = 10;
		}
	
		fuelCost.text = "$" + refuelCost.ToString();
		hullCost.text = "$" + repairCost.ToString();
		carryUpgradeCostText.text = "$" + carryUpgradeCost.ToString();
		fuelUpgradeCostText.text = "$" + fuelUpgradeCost.ToString();
		hullUpgradeCostText.text = "$" + hullUpgradeCost.ToString();
	}
	
	public void HullUpgrade()
	{
	
		if(Main.hullUpgrades < Main.hullUpgradesMax)
		{
			if(Main.cash >= hullUpgradeCost)
			{
				Main.hullUpgrades ++;
				Main.cash += - hullUpgradeCost;
				Main.maxhull += hullUpgrade;
				PlayerPrefs.SetFloat("MaxHull", Main.maxhull);
				hullSlider.maxValue = Main.maxhull;
			}
			
		}
	
	}
	
	public void CarryUpgrade()
	{
		
		if(Main.carryUpgrades < Main.carryUpgradesMax)
		{
			if(Main.cash >= carryUpgradeCost)
			{
				Main.carryUpgrades ++;
				Main.cash += - carryUpgradeCost;
				Main.maxcarry += carryUpgrade;
				PlayerPrefs.SetFloat("MaxCarry", Main.maxcarry);
				
			}
			
		}
		
	}
	
	public void FuelUpgrade()
	{
		
		if(Main.fuelUpgrades < Main.fuelUpgradesMax)
		{
			if(Main.cash >= fuelUpgradeCost)
			{
				Main.fuelUpgrades ++;
				Main.cash += - fuelUpgradeCost;
				Main.maxfuel += fuelUpgrade;
				PlayerPrefs.SetFloat("MaxFuel", Main.maxfuel);
				fuelSlider.maxValue = Main.maxfuel;
			}
			
		}
		
	}
	
	
	
	public void Refuel()
	{
		if (Main.fuel > Main.maxfuel)
		{
			Main.fuel = Main.maxfuel;
		}
		if (Main.fuel < Main.maxfuel)
		{
			if(Main.cash > refuelCost)
			{
				Main.cash += -refuelCost;
				Main.fuel += refuelAmount;
				PlayerPrefs.SetFloat("Fuel", Main.fuel);
			}
		
		}
		
	}
	
	public void Repair()
	{
		if (Main.hull > Main.maxhull)
		{
			Main.hull = Main.maxhull;
		}
		if (Main.hull < Main.maxhull)
		{
			if (Main.cash > repairCost)
			{
				Main.cash += -repairCost;
				Main.hull += repairAmount;
				PlayerPrefs.SetFloat("Hull", Main.hull);
			}
		
		}	
		
	}
	
	void Update()
	{
		fuelUpgradeSlider.value = Main.fuelUpgrades;
		hullUpgradeSlider.value = Main.hullUpgrades;
		carrySlider.value = Main.carryUpgrades;
		fuelSlider.value = Main.fuel;
		hullSlider.value = Main.hull;
		
	}
	
	void OnDisable()
	{
		Main.SetValues();
	
	}
	
}
